package com.blog.blog.util;

import java.util.HashSet;
import java.util.Set;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * 工具：从 Markdown/HTML 内容中提取图片 URL（支持 ![alt](url) 与 <img src="...">）。
 */
public final class ContentImageUtils {
    private static final Pattern MARKDOWN_IMG = Pattern.compile("!\\[[^\\]]*\\]\\(([^)\\s]+)(?:\\s+\"[^\"]*\")?\\)");
    private static final Pattern HTML_IMG = Pattern.compile("<img\\s+[^>]*src=\\\"([^\\\"]+)\\\"[^>]*>", Pattern.CASE_INSENSITIVE);

    private ContentImageUtils() {}

    public static Set<String> extractImageUrls(String content) {
        Set<String> urls = new HashSet<>();
        if (content == null || content.isEmpty()) return urls;

        Matcher m1 = MARKDOWN_IMG.matcher(content);
        while (m1.find()) {
            String url = m1.group(1);
            if (url != null && !url.isBlank()) urls.add(url.trim());
        }
        Matcher m2 = HTML_IMG.matcher(content);
        while (m2.find()) {
            String url = m2.group(1);
            if (url != null && !url.isBlank()) urls.add(url.trim());
        }
        return urls;
    }
}



